package com.amat0urheu.controller.teacherController;

import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.annotation.OperateLog;
import com.amat0urheu.pojo.Case;
import com.amat0urheu.pojo.CaseType;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.CaseService;
import com.amat0urheu.util.generator.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/api")
public class CaseController {
    @Value("${marsctf.uploadPath}")
    private String uploadPath;

    @Value("${marsctf.imagePath}")
    private String imagePath;

    @Value("${marsctf.imageUrl}")
    private String imageUrl;

    @Autowired
    CaseService caseService;
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/user/case/getAllCaseTypes/{pageSize}/{pageNum}")
    ReturnRes getAllCaseTypes(@PathVariable int pageSize,
                              @PathVariable int pageNum) {

        return caseService.getAllCaseTypes(pageSize, pageNum);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/user/cases/{pageSize}/{pageNum}")
    ReturnRes getAllCasesForUser(@PathVariable int pageSize,
                                 @PathVariable int pageNum) {

        return caseService.getAllCases(pageSize, pageNum);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/user/casesByType/{pageSize}/{pageNum}")
    ReturnRes getAllCasesForUserBytypes(@PathVariable int pageSize,
                                 @PathVariable int pageNum,
                                 @RequestParam(required = false) Integer type) {
        if(type==null){
            System.err.println("类型不为空");
        }
        return caseService.getAllCasesBytype(pageSize, pageNum,type);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="更新了案例类型")
    @PostMapping("/admin/case/updateCaseType")
    public ReturnRes updateCaseType(@RequestParam(name = "data") String caseTypeJson) {

        CaseType caseType = JSONObject.parseObject(caseTypeJson, CaseType.class);
        return caseService.updateCaseType(caseType);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="存储案例类型")
    @PostMapping("/admin/case/saveCaseType")
    public ReturnRes saveCaseType(@RequestParam(name = "data") String caseTypeJson) {
        CaseType caseType = JSONObject.parseObject(caseTypeJson, CaseType.class);
        return caseService.saveCaseType(caseType);
    }



    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/case/removeCaseType")
    public ReturnRes removeCaseType(@RequestParam(name = "data") String caseTypeJson) {
        CaseType caseType = JSONObject.parseObject(caseTypeJson, CaseType.class);

        return caseService.removeCaseType(caseType.getId());
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/case/getAllCases/{pageSize}/{pageNum}")
    ReturnRes getAllCases(@PathVariable int pageSize,
                          @PathVariable int pageNum) {

        return caseService.getAllCases(pageSize, pageNum);
    }
/*
*   @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="1",action="更新案例")
    @PostMapping("/admin/case/updateCase")
    public ReturnRes updateCase(@RequestParam(name = "data") String caseJson) {
        Case acase = JSONObject.parseObject(caseJson, Case.class);
        return caseService.updateCase(acase);
    }
* */

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="更新了案例")
    @PostMapping("/admin/case/updateCase")
    public ReturnRes updateCase(@RequestParam("casePicture") MultipartFile file,@RequestParam(name = "data") String caseJson) {
        System.out.println(caseJson);
        if (file.isEmpty()) {
            return new ReturnRes(false, "上传文件不能为空");
        }
        try {
            Case aCase = JSONObject.parseObject(caseJson, Case.class);

            String fileName = file.getOriginalFilename();
            int lastIndexOf = fileName.lastIndexOf(".");
            String ext = lastIndexOf != -1 ? fileName.substring(lastIndexOf + 1) : "";
            String newName = String.valueOf(SnowFlakeUtil.generatorUid()) + "." + ext;
            File dest = new File(imagePath + imageUrl, newName);


            String oldPath=aCase.getCasePicture();
            aCase.setCasePicture(newName);
            return caseService.updateCase(aCase,file,oldPath);
        }catch (Exception e){
            return new ReturnRes(false, "更新出错");
        }

    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="更新了案例")
    @PostMapping("/admin/case/updateCaseNoimage")
    public ReturnRes updateCaseNoimage(@RequestParam(name = "data") String caseJson) {

        try {
            Case aCase = JSONObject.parseObject(caseJson, Case.class);
            return caseService.updateCaseNoimage(aCase);
        }catch (Exception e){

            e.printStackTrace();
            return new ReturnRes(false, "更新出错");
        }

    }




    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="存储案例类型")
    @PostMapping("/admin/case/saveCase")
    public ReturnRes saveCase(@RequestParam(name = "data") String caseJson) {
        Case acase = JSONObject.parseObject(caseJson, Case.class);
        return caseService.saveCase(acase);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/case/removeCase")
    public ReturnRes removeCase(@RequestParam(name = "data") String caseJson) {
        Case acase = JSONObject.parseObject(caseJson, Case.class);
        return caseService.removeCase(acase.getId());
    }

    @GetMapping("/user/case/{id}")
    public ReturnRes getCaseById(@PathVariable Integer id) {
        return caseService.getCaseById(id);
    }

//    @PostMapping("/admin/experiment/uploadImage")
//    public ReturnRes uploadImageForWP(@RequestParam(name = "file") MultipartFile file,
//                                      @RequestParam Integer eid) {
//
//        return caseService.uploadImageForCase(file,eid);//ctfFileService.uploadImageForWP(file, wid);
//    }



    @GetMapping("/admin/caseType/search/{pageSize}/{pageNum}")
    public ReturnRes searchCaseTypeForAdmin(@PathVariable int pageSize,
                                              @PathVariable int pageNum,
                                              @RequestParam String key,
                                              @RequestParam String value) {
        return caseService.searchCaseTypeForAdmin(key, value, pageNum, pageSize);
    }

    @GetMapping("/admin/case/search/{pageSize}/{pageNum}")
    public ReturnRes searchCaseForAdmin(@PathVariable int pageSize,
                                            @PathVariable int pageNum,
                                            @RequestParam String key,
                                            @RequestParam String value) {
        return caseService.searchCaseForAdmin(key, value, pageNum, pageSize);
    }
}

